package com.starter.home.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starter.home.domain.entity.ServeOrder;
import com.starter.home.domain.entity.User;
import com.starter.home.service.IServeOrderService;
import com.starter.home.util.CommonParam;
import com.starter.home.util.PageUtil;
import com.starter.home.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-05-01
 */
@RestController
@RequestMapping("/order")
public class ServeOrderController {

    @Autowired
    private IServeOrderService orderService;

    @GetMapping(value = "/getList")
    public ResponseResult getList(@ModelAttribute CommonParam commonParam,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (Objects.isNull(user)){
            return ResponseResult.success(null);
        }
        Integer userId = user.getId();
        PageUtil pageUtil = orderService.getList(userId,commonParam);
        return ResponseResult.success(pageUtil);
    }

    @PostMapping(value = "/{id}/{status}")
    public ResponseResult change(@PathVariable Integer id,@PathVariable Integer status){
        // 0：未支付，1：已经支付，2：已同意，代表翼9订单。10关注,8已经支付
        ServeOrder serveOrder = orderService.getBaseMapper().selectById(id);
        serveOrder.setStatus(status);
        int i = orderService.getBaseMapper().updateById(serveOrder);
        if ( i > 0){
            return ResponseResult.success(null);
        }else{
            return ResponseResult.error(null);
        }
    }
    @PostMapping(value = "/buyAll")
    public ResponseResult buyAll(@RequestBody Integer[] arr, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user == null){
            return ResponseResult.error("登录过期，请重新登录");
        }
        for (Integer integer : arr) {
            int i = orderService.updateOrder(integer,2);
        }
        return ResponseResult.success(null);
    }

    @PostMapping(value = "/save/{id}/{status}")
    public ResponseResult save(@PathVariable Integer id,@PathVariable Integer status,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user == null){
            return ResponseResult.error("登录过期，请重新登录");
        }
        QueryWrapper<ServeOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        queryWrapper.eq("info_id",id);
        queryWrapper.eq("status",status);
        if (orderService.getBaseMapper().selectList(queryWrapper).size() > 0) {
            return ResponseResult.error("你已经添加过了，无需添加");
        }
        ServeOrder order = new ServeOrder();
        order.setInfoId(id);
        order.setUserId(user.getId());
        order.setStatus(status);
        order.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        int i = orderService.getBaseMapper().insert(order);
        if ( i > 0){
            return ResponseResult.success(null);
        }else{
            return ResponseResult.error("新增失败");
        }
    }

    @PostMapping(value = "/delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        int i = orderService.getBaseMapper().deleteById(id);
        if ( i > 0){
            return ResponseResult.success(null);
        }else{
            return ResponseResult.error(null);
        }
    }



}
